package com.example.dogliving.activity;

import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.BottomNavigationView.OnNavigationItemSelectedListener;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;

import com.example.dogliving.R;
import com.example.dogliving.fragment.column.ColumnFragment;
import com.example.dogliving.fragment.home.HomeFragment;
import com.example.dogliving.fragment.living.LivingFragment;
import com.example.dogliving.fragment.my.MyFragment;
import com.example.dogliving.fragment.news.NewsFragment;
import com.yztc.core.App;
import com.yztc.core.utils.ToastUtils;

import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {
    private static final String SELINDEX = "INDEX";
    private BottomNavigationView mainBar;
    private FragmentManager fragmentManager;

    private String[] tags = {"HOMEFRAGMENT", "COLUMNFRAGMENT", "LIVINGFRAGMENT", "NEWSFRAGMENT", "MYFRAGMENT"};
    private int selindex = 0;
    private Fragment homeFragment, columnFragment, livingFragment, newsFragment, myFragmnet;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);




        mainBar= (BottomNavigationView) findViewById(R.id.main_Bar);
//        getSupportActionBar().hide();
        fragmentManager = getSupportFragmentManager();
        if (savedInstanceState != null) {
            //读取上一次界面Save的时候tab选中的状态
            selindex = savedInstanceState.getInt(SELINDEX, selindex);
            homeFragment = fragmentManager.findFragmentByTag(tags[0]);
            columnFragment = fragmentManager.findFragmentByTag(tags[1]);
            livingFragment = fragmentManager.findFragmentByTag(tags[2]);
            newsFragment = fragmentManager.findFragmentByTag(tags[3]);
            myFragmnet = fragmentManager.findFragmentByTag(tags[4]);
            restoreSelect();
        }
        mainBar.setOnNavigationItemSelectedListener(new SelectedListener());
        mainBar.findViewById(R.id.menu_home).performClick();

    }

    private void restoreSelect() {
        View view = null;
        switch (selindex) {
            case 0:
                view = mainBar.findViewById(R.id.menu_home);
                break;
            case 1:
                view = mainBar.findViewById(R.id.menu_gamelist);
                break;
            case 2:
                view = mainBar.findViewById(R.id.menu_video);
                break;
            case 3:
                view = mainBar.findViewById(R.id.menu_news);
                break;
            case 4:
                view = mainBar.findViewById(R.id.menu_i);
                break;
        }
        view.performClick();
    }

    class SelectedListener implements OnNavigationItemSelectedListener {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            FragmentTransaction ft = fragmentManager.beginTransaction();
            switch (item.getItemId()) {
                case R.id.menu_home:
                    selindex = 0;
                    hint(ft, 1, 2, 3, 4);
                    if (homeFragment == null) {
                        homeFragment = new HomeFragment();
                        ft.add(R.id.main_frameLayout, homeFragment, tags[0]);
                    } else {
                        ft.show(homeFragment);
                    }
                    break;
                case R.id.menu_gamelist:
                    selindex = 1;
                    hint(ft, 0, 2, 3, 4);
                    if (columnFragment == null) {
                        columnFragment = new ColumnFragment();
                        ft.add(R.id.main_frameLayout, columnFragment, tags[1]);
                    } else {
                        ft.show(columnFragment);
                    }
                    break;
                case R.id.menu_video:
                    selindex = 2;
                    hint(ft, 0, 1, 3, 4);
                    if (livingFragment == null) {
                        livingFragment = new LivingFragment();
                        ft.add(R.id.main_frameLayout, livingFragment, tags[2]);
                    } else {
                        ft.show(livingFragment);
                    }
                    break;
                case R.id.menu_news:
                    selindex = 3;
                    hint(ft, 0, 1, 2, 4);
                    if (newsFragment == null) {
                        newsFragment = new NewsFragment();
                        ft.add(R.id.main_frameLayout, newsFragment, tags[3]);
                    } else {
                        ft.show(newsFragment);
                    }
                    break;
                case R.id.menu_i:
                    selindex = 4;
                    hint(ft, 0, 1, 2, 3);
                    if (myFragmnet == null) {
                        myFragmnet = new MyFragment();
                        ft.add(R.id.main_frameLayout, myFragmnet, tags[4]);
                    } else {
                        ft.show(myFragmnet);
                    }
                    break;
            }
            ft.commit();
            return true;
        }
    }

    private void hint(FragmentTransaction ft, Integer... fs) {
        for (int j = 0; j < fs.length; j++) {
            switch (fs[j]) {
                case 0:
                    if (homeFragment != null)
                        ft.hide(homeFragment);
                    break;
                case 1:
                    if (columnFragment != null)
                        ft.hide(columnFragment);
                    break;
                case 2:
                    if (livingFragment != null)
                        ft.hide(livingFragment);
                    break;
                case 3:
                    if (newsFragment != null)
                        ft.hide(newsFragment);
                    break;
                case 4:
                    if (myFragmnet != null)
                        ft.hide(myFragmnet);
                    break;
            }
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState,outPersistentState);
        outState.putInt(SELINDEX, selindex);
    }

    long curr;
    @Override
    public void onBackPressed() {
        if (System.currentTimeMillis() - curr > 2000) {
            curr = System.currentTimeMillis();
            ToastUtils.show("再按一次退出");
        } else {
            App.getContext().onDestory();
            super.onBackPressed();
        }
    }
}
